package com.cicada.cocos2dsamples.samples;

import java.util.Random;

import org.cocos2d.actions.base.CCRepeatForever;
import org.cocos2d.actions.instant.CCCallFuncN;
import org.cocos2d.actions.interval.CCAnimate;
import org.cocos2d.actions.interval.CCFadeOut;
import org.cocos2d.actions.interval.CCIntervalAction;
import org.cocos2d.actions.interval.CCJumpTo;
import org.cocos2d.actions.interval.CCMoveTo;
import org.cocos2d.actions.interval.CCRepeat;
import org.cocos2d.actions.interval.CCSequence;
import org.cocos2d.layers.CCLayer;
import org.cocos2d.nodes.CCAnimation;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.nodes.CCSpriteFrameCache;
import org.cocos2d.nodes.CCSpriteSheet;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGSize;

public class Paladog extends BaseActivity {

	@Override
	public CCLayer getLayer() {
		return new Layer();
	}

	static class Layer extends CCLayer {
		public CGSize winSize = CCDirector.sharedDirector().winSize();
		CCAnimate zombie_attack;
		CCAnimate zombie_walking;
		CCAnimate zombie_dead;
		CCAnimate ogre_attack;
		CCAnimate ogre_walking;
		CCAnimate ogre_dead;
		CCAnimate devil_attack;
		CCAnimate devil_walking;
		CCAnimate devil_dead;
		Random random;

		public Layer() {
			random = new Random();
			initZombieAnimation();
			initOgreAnimation();
			initDevilAnimation();
			schedule("monsters", 0.3f);
		}

		public void monsters(float dt) {
			int monster = random.nextInt(3);
			if (monster == 0) {
				createZombie();
			} else if (monster == 1) {
				createOgre();
			} else {
				createDevil();
			}
		}

		public void initZombieAnimation() {
			CCSpriteFrameCache cache = CCSpriteFrameCache
					.sharedSpriteFrameCache();
			cache.addSpriteFrames("e33_01.plist");
			CCSpriteSheet.spriteSheet("e33_01.png");

			CCAnimation animation_attack = CCAnimation.animation(
					"zombie_attack", 0.05f);
			CCAnimation animation_walking = CCAnimation.animation(
					"zombie_walking", 0.05f);
			CCAnimation animation_dead = CCAnimation.animation("zombie_dead",
					0.05f);
			for (int i = 0; i < 16; i++) {

				if (i < 16) {
					animation_dead.addFrame(cache.getSpriteFrame(String.format(
							"e33_down_%04d.png", i + 1)));
				}
				if (i < 15) {
					animation_attack.addFrame(cache.getSpriteFrame(String
							.format("e33_att_%04d.png", i + 1)));
				}

				if (i < 6) {
					animation_walking.addFrame(cache.getSpriteFrame(String
							.format("e33_walk_%04d.png", i + 1)));
				}
			}

			zombie_attack = CCAnimate.action(animation_attack);
			zombie_walking = CCAnimate.action(animation_walking);
			zombie_dead = CCAnimate.action(animation_dead);
		}

		public void createZombie() {
			CCSprite zombie = CCSprite.sprite(zombie_walking.getAnimation()
					.frames().get(0));
			zombie.setFlipX(true);
			float posY = random.nextInt((int) (winSize.getHeight() - zombie
					.getContentSize().height * 2))
					+ (zombie.getContentSize().height);
			zombie.setPosition(zombie.getContentSize().width / 2, posY);
			addChild(zombie);

			CCMoveTo movetohalf = CCMoveTo.action(3.0f,
					CGPoint.ccp(winSize.getWidth() / 2, posY));
			CCSequence sequence = CCSequence.actions(movetohalf,
					CCCallFuncN.action(this, "zombieMoveHalfFinished"));
			zombie.runAction(CCRepeatForever.action(zombie_walking.copy()));
			zombie.runAction(sequence);
		}

		public void zombieMoveHalfFinished(Object node) {
			CCSprite zombie = (CCSprite) node;
			zombie.stopAllActions();

			CCSequence sequence = CCSequence.actions(
					CCRepeat.action(zombie_attack.copy(), 3),
					CCCallFuncN.action(this, "zombieAttackFinished"));
			zombie.runAction(sequence);

		}

		public void zombieAttackFinished(Object node) {
			CCSprite zombie = (CCSprite) node;
			zombie.stopAllActions();

			CCMoveTo movetoend = CCMoveTo.action(
					3.0f,
					CGPoint.ccp(winSize.getWidth()
							- zombie.getContentSize().width,
							zombie.getPosition().y));
			CCSequence sequence = CCSequence.actions(movetoend,
					CCCallFuncN.action(this, "zombieMoveEndFinished"));
			zombie.runAction(CCRepeatForever.action(zombie_walking.copy()));
			zombie.runAction(sequence);
		}

		public void zombieMoveEndFinished(Object node) {
			CCSprite zombie = (CCSprite) node;
			zombie.stopAllActions();

			CCSequence sequence = CCSequence.actions(zombie_dead.copy(),
					 CCCallFuncN.action(this, "zombieDeadFinished"));
			
			zombie.runAction(sequence);
		}

		public void zombieDeadFinished(Object node) {
			CCSprite zombie = (CCSprite) node;
			zombie.removeSelf();

		}

		public void initOgreAnimation() {
			CCSpriteFrameCache cache = CCSpriteFrameCache
					.sharedSpriteFrameCache();
			cache.addSpriteFrames("e35_01.plist");
			CCSpriteSheet.spriteSheet("e35_01.png");

			CCAnimation animation_attack = CCAnimation.animation("ogre_attack",
					0.05f);
			CCAnimation animation_walking = CCAnimation.animation(
					"ogre_walking", 0.05f);
			CCAnimation animation_dead = CCAnimation.animation("ogre_dead",
					0.05f);
			for (int i = 0; i < 16; i++) {

				if (i < 16) {
					animation_dead.addFrame(cache.getSpriteFrame(String.format(
							"e35_down_%04d.png", i + 1)));
				}
				if (i < 10) {
					animation_attack.addFrame(cache.getSpriteFrame(String
							.format("e35_att_%04d.png", i + 1)));
				}

				if (i < 12) {
					animation_walking.addFrame(cache.getSpriteFrame(String
							.format("e35_walk_%04d.png", i + 1)));
				}
			}

			ogre_attack = CCAnimate.action(animation_attack);
			ogre_walking = CCAnimate.action(animation_walking);
			ogre_dead = CCAnimate.action(animation_dead);
		}

		public void createOgre() {
			CCSprite ogre = CCSprite.sprite(ogre_walking.getAnimation()
					.frames().get(0));
			ogre.setFlipX(true);
			float posY = random.nextInt((int) (winSize.getHeight() - ogre
					.getContentSize().height * 2))
					+ (ogre.getContentSize().height);
			ogre.setPosition(ogre.getContentSize().width / 2, posY);
			addChild(ogre);

			CCMoveTo movetohalf = CCMoveTo.action(3.0f,
					CGPoint.ccp(winSize.getWidth() / 2, posY));
			CCSequence sequence = CCSequence.actions(movetohalf,
					CCCallFuncN.action(this, "ogreMoveHalfFinished"));
			ogre.runAction(CCRepeatForever.action(ogre_walking.copy()));
			ogre.runAction(sequence);
		}

		public void ogreMoveHalfFinished(Object node) {
			CCSprite zombie = (CCSprite) node;
			zombie.stopAllActions();

			CCSequence sequence = CCSequence.actions(
					CCRepeat.action(ogre_attack.copy(), 3),
					CCCallFuncN.action(this, "ogreAttackFinished"));
			zombie.runAction(sequence);

		}

		public void ogreAttackFinished(Object node) {
			CCSprite ogre = (CCSprite) node;
			ogre.stopAllActions();

			CCMoveTo movetoend = CCMoveTo.action(3.0f, CGPoint.ccp(
					winSize.getWidth() - ogre.getContentSize().width,
					ogre.getPosition().y));
			CCSequence sequence = CCSequence.actions(movetoend,
					CCCallFuncN.action(this, "ogreMoveEndFinished"));
			ogre.runAction(CCRepeatForever.action(ogre_walking.copy()));
			ogre.runAction(sequence);
		}

		public void ogreMoveEndFinished(Object node) {
			CCSprite ogre = (CCSprite) node;
			ogre.stopAllActions();

			
			CCSequence sequence = CCSequence.actions(ogre_dead.copy(), 
					CCCallFuncN.action(this, "ogreDeadFinished"));
			
			ogre.runAction(sequence);
		}

		public void ogreDeadFinished(Object node) {
			CCSprite ogre = (CCSprite) node;
			ogre.removeSelf();

		}

		public void initDevilAnimation() {
			CCSpriteFrameCache cache = CCSpriteFrameCache
					.sharedSpriteFrameCache();
			cache.addSpriteFrames("e04_01.plist");
			CCSpriteSheet.spriteSheet("e04_01.png");

			CCAnimation animation_attack = CCAnimation.animation(
					"devil_attack", 0.05f);
			CCAnimation animation_walking = CCAnimation.animation(
					"devil_walking", 0.05f);
			CCAnimation animation_dead = CCAnimation.animation("devil_dead",
					0.05f);
			for (int i = 0; i < 16; i++) {
				if (i < 8) {
					animation_walking.addFrame(cache.getSpriteFrame(String
							.format("e04_walk_%04d.png", i + 1)));
				}

				if (i < 10) {
					animation_attack.addFrame(cache.getSpriteFrame(String
							.format("e04_att_%04d.png", i + 1)));
				}

				if (i < 16) {
					animation_dead.addFrame(cache.getSpriteFrame(String.format(
							"e04_down_%04d.png", i + 1)));
				}

			}

			devil_attack = CCAnimate.action(animation_attack);
			devil_walking = CCAnimate.action(animation_walking);
			devil_dead = CCAnimate.action(animation_dead);
		}

		public void createDevil() {
			CCSprite devil = CCSprite.sprite(devil_walking.getAnimation()
					.frames().get(0));
			devil.setFlipX(true);
			float posY = random.nextInt((int) (winSize.getHeight() - devil
					.getContentSize().height * 2))
					+ (devil.getContentSize().height);
			devil.setPosition(devil.getContentSize().width / 2, posY);
			addChild(devil);

			CCMoveTo movetohalf = CCMoveTo.action(3.0f,
					CGPoint.ccp(winSize.getWidth() / 2, posY));
			CCSequence sequence = CCSequence.actions(movetohalf,
					CCCallFuncN.action(this, "devilMoveHalfFinished"));
			devil.runAction(CCRepeatForever.action(devil_walking.copy()));
			devil.runAction(sequence);
		}

		public void devilMoveHalfFinished(Object node) {
			CCSprite devil = (CCSprite) node;
			devil.stopAllActions();

			CCSequence sequence = CCSequence.actions(
					CCRepeat.action(devil_attack.copy(), 3),
					CCCallFuncN.action(this, "devilAttackFinished"));
			devil.runAction(sequence);

		}

		public void devilAttackFinished(Object node) {
			CCSprite devil = (CCSprite) node;
			devil.stopAllActions();

			CCMoveTo movetoend = CCMoveTo.action(3.0f, CGPoint.ccp(
					winSize.getWidth() - devil.getContentSize().width,
					devil.getPosition().y));
			CCSequence sequence = CCSequence.actions(movetoend,
					CCCallFuncN.action(this, "devilMoveEndFinished"));
			devil.runAction(CCRepeatForever.action(devil_walking.copy()));
			devil.runAction(sequence);
		}

		public void devilMoveEndFinished(Object node) {
			CCSprite devil = (CCSprite) node;
			devil.stopAllActions();
			CCSequence sequence = CCSequence.actions(devil_dead.copy(),
			 CCCallFuncN.action(this, "devilDeadFinished"));
			
			devil.runAction(sequence);
		}

		public void devilDeadFinished(Object node) {
			CCSprite devil = (CCSprite) node;
			devil.removeSelf();

		}
	}
}
